package com.lechuang.goods.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lechuang.common.request.PageRequest;
import com.lechuang.common.result.PageResult;
import com.lechuang.common.result.Result;
import com.lechuang.common.result.ResultEnum;
import com.lechuang.goods.dto.FreightTemplateListDTO;
import com.lechuang.goods.dto.FreightSaveTemplateDTO;
import com.lechuang.goods.entity.FreightTemplate;
import com.lechuang.goods.manager.FreightTemplateManager;
import com.lechuang.goods.service.FreightTemplateService;
import com.lechuang.goods.vo.FreightTemplateDetailVO;
import com.lechuang.goods.vo.FreightTemplateListVO;
import com.lechuang.system.feign.TenantInfoFeign;
import com.lechuang.system.vo.TenantListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FreightTemplateManagerImpl implements FreightTemplateManager {

    @Resource
    private FreightTemplateService freightTemplateService;
    @Resource
    private TenantInfoFeign tenantInfoFeign;

    @Override
    public PageResult<FreightTemplateListVO> pageList(PageRequest<FreightTemplateListDTO> pageRequest) {
        FreightTemplateListDTO query = pageRequest.getQuery();
        List<TenantListVO> tenantList = new ArrayList<>();
        Result<List<TenantListVO>> byNameOrPhone = tenantInfoFeign.getByNameOrPhone( query.getSelectText());
        if(Objects.equals(byNameOrPhone.getCode(), ResultEnum.SUCCEED.getCode())){
            tenantList = byNameOrPhone.getData();
        }
        if(CollUtil.isEmpty(tenantList)){
            return PageResult.result(new ArrayList<>(), new Page<>(), pageRequest, null);
        }
        Map<Long, TenantListVO> tenantMap = tenantList.stream().collect(Collectors.toMap(TenantListVO::getId, a -> a));
        Page<FreightTemplateListVO> page = freightTemplateService.pageList(pageRequest, tenantMap.keySet());
        List<FreightTemplateListVO> records = page.getRecords();
        records.forEach(bean -> {
            if(tenantMap.containsKey(bean.getTenantId())){
                TenantListVO tenantListVO = tenantMap.get(bean.getTenantId());
                bean.setTenantName(tenantListVO.getName())
                        .setTenantPhone(tenantListVO.getPhone());
            }
        });
        return PageResult.result(records, page, pageRequest, null);
    }

    @Override
    public void saveOrUpdate(FreightSaveTemplateDTO freightSaveTemplateDTO) {
        Long id = freightSaveTemplateDTO.getId();
        if(id == null){
            this.save(freightSaveTemplateDTO);
        }else{
            this.update(freightSaveTemplateDTO);
        }

    }

    @Override
    public void delete(Long id) {
        freightTemplateService.removeById(id);
    }

    @Override
    public FreightTemplateDetailVO detail(Long id) {
        FreightTemplate freightTemplate = freightTemplateService.getById(id);
        return BeanUtil.copyProperties(freightTemplate, FreightTemplateDetailVO.class);
    }

    private void update(FreightSaveTemplateDTO freightSaveTemplateDTO) {
        FreightTemplate freightTemplate = new FreightTemplate()
                .setTenantId(freightSaveTemplateDTO.getTenantId())
                .setName(freightSaveTemplateDTO.getName())
                .setType(freightSaveTemplateDTO.getType())
                .setLogisticsName(freightSaveTemplateDTO.getLogisticsName())
                .setFreeShipping(freightSaveTemplateDTO.getFreeShipping())
                .setFreeAmount(freightSaveTemplateDTO.getFreeAmount())
                .setFirstWeightNum(freightSaveTemplateDTO.getFirstWeightNum())
                .setFirstWeightAmount(freightSaveTemplateDTO.getFirstWeightAmount())
                .setContinuationWeightNum(freightSaveTemplateDTO.getContinuationWeightNum())
                .setContinuationWeightAmount(freightSaveTemplateDTO.getContinuationWeightAmount())
                .setTemplateStatus(freightSaveTemplateDTO.getTemplateStatus());
        freightTemplate.setId(freightSaveTemplateDTO.getId());
        freightTemplateService.updateById(freightTemplate);
    }

    private void save(FreightSaveTemplateDTO freightSaveTemplateDTO) {
        FreightTemplate freightTemplate = new FreightTemplate()
                .setTenantId(freightSaveTemplateDTO.getTenantId())
                .setName(freightSaveTemplateDTO.getName())
                .setType(freightSaveTemplateDTO.getType())
                .setLogisticsName(freightSaveTemplateDTO.getLogisticsName())
                .setFreeShipping(freightSaveTemplateDTO.getFreeShipping())
                .setFreeAmount(freightSaveTemplateDTO.getFreeAmount())
                .setFirstWeightNum(freightSaveTemplateDTO.getFirstWeightNum())
                .setFirstWeightAmount(freightSaveTemplateDTO.getFirstWeightAmount())
                .setContinuationWeightNum(freightSaveTemplateDTO.getContinuationWeightNum())
                .setContinuationWeightAmount(freightSaveTemplateDTO.getContinuationWeightAmount());
        freightTemplateService.save(freightTemplate);
    }

}
